package com.spring.mvc.controller;

import com.spring.mvc.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/9/3 20:42
 * @Description
 */
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        // 传递数据
        request.setAttribute("title", "th:text设置文本内容");
        request.setAttribute("val", "th:value设置表单值");
        request.setAttribute("checked", "true");
        request.setAttribute("selected", "true");
        request.setAttribute("boolVal", "true");
        request.setAttribute("cond", "2");
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setUsername("uname00" + i);
            user.setPassword("12300" + i);
            list.add(user);
        }
        request.setAttribute("list", list);
        // 返回具体的 index.html 页面
        // SpringMVC 框架会进行拼接: /WEB-INF/pages/ index .html
        return "index";
    }

    @RequestMapping("/param")
    public String index2(HttpServletRequest request) {
        // 接收数据
        String id = request.getParameter("id");
        System.out.println(id);
        return "index";
    }

}
